package game;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * A ticking bomb
 * @author Simon
 *
 */
public class Bomb implements ArenaObject {
	int tile_x, tile_y;
	
	public static final int ANIM_DELAY = 5;
	
	static Image image = null;
	Sprite gfx = null;
	int countdown;
	int frameDelay;
	boolean dead;
	
	public Bomb(int tile_x, int tile_y) {
		if (image == null) {
			try {
				image = Image.createImage("/gfx/dynamite.png");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		gfx = new Sprite(image, Arena.TILESIZE, Arena.TILESIZE);
		Arena.Position(gfx, tile_x, tile_y);
		this.tile_x = tile_x;
		this.tile_y = tile_y;
		countdown = 100;
		frameDelay = ANIM_DELAY;
		dead = false;
	}
	
	public void advance() {
		if (dead) return;
		countdown--;
		frameDelay--;
		if (frameDelay <= 0) {
			frameDelay = ANIM_DELAY;
			gfx.nextFrame();
		}
		if (countdown <= 0) {
			dead = true;
		}
	}
	
	public boolean isDead() {
		return dead;
	}
	
	public void draw(Graphics g) {
		if (!dead) gfx.paint(g);
	}
}
